/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mongodb.repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

/**
 * xxx
 *
 * @author Wangyi
 * @version v1.0
 *
 * @date 2013-10-14
 *
 */
public class StringToDateConverter implements Converter<String, Date> {
	
	private static final String[] formats={"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","EEE MMM dd HH:mm:ss z yyyy"};
	
	@Override
	public Date convert(String source) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.setLenient(false);
		Date target=null;
		for(String fmt : formats){
			try {
				sdf.applyPattern(fmt);
	            target=sdf.parse(source);
	            break;
	        } catch (ParseException e) {
	            continue;
	        }
		}
        return target;
	}
}
